package com.ruoyi.web.controller.tool.designpattern.creationalPatterns.builder;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

// 1. Product (抽象或具体的产品类)
class Pizza {
    private String dough;
    private String sauce;
    private List<String> toppings;

    // 私有构造器确保只能通过建造者来构建对象
    private Pizza(PizzaBuilder builder) {
        this.dough = builder.dough;
        this.sauce = builder.sauce;
        this.toppings = new ArrayList<>(builder.toppings);
    }

    // 提供获取信息的方法
    public String getDough() {
        return dough;
    }

    public String getSauce() {
        return sauce;
    }

    public List<String> getToppings() {
        return Collections.unmodifiableList(toppings);
    }

    // 建造者类
    public static class PizzaBuilder {
        private String dough = "默认面团";
        private String sauce = "默认酱料";
        private List<String> toppings = new ArrayList<>();

        // 设置各个部件的方法
        public PizzaBuilder setDough(String dough) {
            this.dough = dough;
            return this;
        }

        public PizzaBuilder setSauce(String sauce) {
            this.sauce = sauce;
            return this;
        }

        public PizzaBuilder addTopping(String topping) {
            this.toppings.add(topping);
            return this;
        }

        // 构建并返回最终的Pizza对象
        public Pizza build() {
            return new Pizza(this);
        }
    }
}

/**
 * 建造者模式在Java中的实现例子，
 * 我们可以创建一个简单的披萨制作场景。
 * 在这个场景中，我们有Pizza（产品）和PizzaBuilder（建造者）两个主要角色。
 */
public class Test {
    public static void main(String[] args) {
        // 使用建造者构建披萨
        Pizza pizza = new Pizza.PizzaBuilder()
                .setDough("薄饼面团")
                .setSauce("番茄酱")
                .addTopping("奶酪")
                .addTopping("意大利辣香肠")
                .addTopping("蘑菇")
                .build();

        // 输出披萨的详细信息
        System.out.println("面团类型: " + pizza.getDough());
        System.out.println("酱料类型: " + pizza.getSauce());
        System.out.println("配料列表: " + pizza.getToppings());
    }
}
